﻿<?php

	class Session 
	{
		public static function init($time_in_days=1)
		{
			
			ini_set('session.gc-maxlifetime', $time_in_days * 24 * 60);
			@session_start();
			if (!isset($_SESSION))
			{
			
				Session::set('sessionid', session_id());
				Session::set('selection', array());
				
			}
		}
		
		public static function set($key, $item=NULL)
		{
			$_SESSION[ $key ] = $item;
		}
		
		public static function get($key)
		{
			//var_dump(isset($_SESSION[ $key ]) , !empty($_SESSION[ $key ]) , $_SESSION[ $key ] !== NULL); //exit;
			// SESSION is not  initialized !!??
			
			return isset($_SESSION[ $key ]) && !empty($_SESSION[ $key ]) && $_SESSION[ $key ] !== NULL ? $_SESSION[ $key ] : FALSE;
		}
	
		public static function isUserAuthenticated()
		{
			return Session::get('user') !== FALSE;
		}
		
		public static function checkUserInfo($info)
		{
			
			$phone = @$info['phone'];
			$password = @$info['password'];
			$remember = @$info['remember'];

			if ( DB::getUserAccount( $info ) !== FALSE )
			{
				Session::set('user', $info);
				Utils::redirect('home');
			}
			else
			{
				Session::set('feedback', array('type'=>'error', 
																				'message'=>'Your login/password are invalid!', 
																				'description' => 'You entered a wrong login or password. Please check again your credentials!'));
				//Utils::redirectLogin();
			}

		}
		
		public static function showLastFeedback()
		{
			$feedback = Session::get('feedback');
			$message = '';
			if (is_array($feedback))
			{
				$message = '<div class="feedback '.$feedback['type'].'">
					<h3>'.$feedback['message'].'</h3>
					<p>'.$feedback['description'].'</p>
				</div>';
				Session::set('feedback');
			}
			return $message;
			
		}

		public static function dump()
		{
			var_dump($_SESSION);
		} 
	
	}

?>
